# 虽说写得不咋好，也可能不会有其他同类型的东西了，但反正是投入心血了的，还是上传一下吧
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

print('----实验一-----')


def fun(u_1, u_2, u_3, u_4):
    return (abs(u_1) + abs(u_2) + abs(u_3) + abs(u_4)) / 4


def fun_(k_h):
    return (k_h[5] + k_h[6] + k_h[7] + k_h[8] + k_h[9] + k_h[0] + k_h[1] + k_h[2] + k_h[3] + k_h[4]) / 10


C = float(input("请输入您的线圈常数（单位：mT/A）："))
I_m = 300
B = C * I_m * (10 ** -3)
print(f"磁场大小为{B}mT")
filename_1 = "E:/data_1.csv"
df_1 = pd.read_csv(filename_1)
I_s = np.array(df_1.iloc[:, 0].values)
U_CD1 = np.array(df_1.iloc[:, 1].values)
U_CD2 = np.array(df_1.iloc[:, 2].values)
U_CD3 = np.array(df_1.iloc[:, 3].values)
U_CD4 = np.array(df_1.iloc[:, 4].values)
U_H = fun(U_CD1, U_CD2, U_CD3, U_CD4)
K_H = U_H / (I_s * B)
K_H = fun_(K_H)
print(f"实验一测得霍尔电压U_H分别为:{U_H}(单位:mV)\n平均霍尔元件灵敏度K_H为{K_H}[单位:mV/(mA*mT)]")
k, b = np.polyfit(I_s, U_H, 1)
loss = [abs(k * I_s[i] + b - U_H[i]) for i in range(len(I_s))]
loss_to = sum(loss)
print(f"k:{k},b:{b},loss:{loss_to}")
plt.scatter(I_s, U_H)
plt.plot(I_s, k * I_s + b, color='red')
plt.title("Experiment One")
plt.savefig('C:/Users/admin/Desktop/实验一.png', dpi=500, bbox_inches='tight')
plt.show()
# ————————————————————————实验二——————————————————————————————————————————
I_s = 3.00
filename_2 = "E:/data_2.csv"
df_2 = pd.read_csv(filename_2)
I_m = np.array(df_2.iloc[:, 0].values)
U_CD1 = np.array(df_2.iloc[:, 1].values)
U_CD2 = np.array(df_2.iloc[:, 2].values)
U_CD3 = np.array(df_2.iloc[:, 3].values)
U_CD4 = np.array(df_2.iloc[:, 4].values)
U_H = fun(U_CD1, U_CD2, U_CD3, U_CD4)
B = U_H / (I_s * K_H)
print(f"实验二测得霍尔电压U_H分别为:{U_H}(单位:mV)\n磁场强度B分别为{B}(单位:mT)")
k, b = np.polyfit(I_m, U_H, 1)
loss = [abs(k * I_m[i] + b - U_H[i]) for i in range(len(I_m))]
loss_to = sum(loss)
print(f"k:{k},b:{b},loss:{loss_to}")
plt.scatter(I_m, U_H)
plt.plot(I_m, k * I_m + b, color='red')
plt.title("Experiment Two")
plt.savefig('C:/Users/admin/Desktop/实验二.png', dpi=500, bbox_inches='tight')
plt.show()
# ————————————————————————实验三——————————————————————————————————————————
filename = "E:/data_3.csv"
df_3 = pd.read_csv(filename)
U_CD1_1 = np.array(df_3.iloc[:, 1].values)
U_CD2_1 = np.array(df_3.iloc[:, 2].values)
U_CD3_1 = np.array(df_3.iloc[:, 3].values)
U_CD4_1 = np.array(df_3.iloc[:, 4].values)
U_H_1 = fun(U_CD1_1, U_CD2_1, U_CD3_1, U_CD4_1)
U_CD1_2 = np.array(df_3.iloc[:, 5].values)
U_CD2_2 = np.array(df_3.iloc[:, 6].values)
U_CD3_2 = np.array(df_3.iloc[:, 7].values)
U_CD4_2 = np.array(df_3.iloc[:, 8].values)
U_H_2 = fun(U_CD1_2, U_CD2_2, U_CD3_2, U_CD4_2)
U_CD1_3 = np.array(df_3.iloc[:, 9].values)
U_CD2_3 = np.array(df_3.iloc[:, 10].values)
U_CD3_3 = np.array(df_3.iloc[:, 11].values)
U_CD4_3 = np.array(df_3.iloc[:, 12].values)
U_H_3 = fun(U_CD1_3, U_CD2_3, U_CD3_3, U_CD4_3)
U_H = np.array([U_H_1, U_H_2, U_H_3])
I_s = 5.00
B = U_H / (I_s * K_H)
print(f"实验三测得霍尔电压U_H分别为:{U_H}(单位:mV)\n磁场强度B分别为{B}(单位:mT)")
fig = plt.figure()  # 定义新的三维坐标轴
ax = plt.axes(projection='3d')

# 定义三维数据
xx = np.array(df_3.iloc[:, 0].values)
yy = np.array([0, 10, 20])
X, Y = np.meshgrid(xx, yy)
Z = np.array(B)
# 作图
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
plt.xlabel("x")
plt.ylabel("y")
plt.title("3D surface plot")
plt.savefig('C:/Users/admin/Desktop/实验三.png', dpi=500, bbox_inches='tight')
plt.show()
